<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\InstitutionFormat;
use App\Format\LeaderProfileFormat;
use App\Format\QingfengProposalFormat;
use App\Http\Requests\CreateInstitutionRequest;
use App\Http\Requests\CreateLeaderProfileRequest;
use App\Http\Requests\CreateQingfengProposalRequest;
use App\Services\QingfengService;
use Illuminate\Http\Request;

class QingfengController extends Controller
{
    protected $qingfengService;

    public function __construct(QingfengService $qingfengService)
    {
        $this->qingfengService = $qingfengService;
    }

    public function getLeaders()
    {
        $res = $this->qingfengService->leaders();
        return renderSuccessJson($res);
    }

    public function getAdminLeaderList(Request $request)
    {
        $search = [];

        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }

        if ($request->input('post')) {
            $search['post'] = $request->input('post');
        }

        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);

        $res = $this->qingfengService->leaderAdminList($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function deleteLeader(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->qingfengService->leaderDelete($id);
        return renderSuccessJson();
    }

    public function updateLeader(Request $request)
    {
        $format = new LeaderProfileFormat($request->all());
        $this->qingfengService->updateLeader($format);
        return renderSuccessJson();
    }

    public function setLeaderSort(Request $request)
    {
        $id = $request->input('id');
        $sort = $request->input('sort');
        $this->qingfengService->leaderSort($id, $sort);
        return renderSuccessJson();
    }

    public function getInstitutions()
    {
        $res = $this->qingfengService->institutions();
        return renderSuccessJson($res);
    }

    public function getAdminInstitutions(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }

        $res = $this->qingfengService->institutionAdminList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function createLeader(CreateLeaderProfileRequest $createLeaderProfileRequest)
    {
        $format = new LeaderProfileFormat($createLeaderProfileRequest->all());
        $this->qingfengService->createLeader($format);
        return renderSuccessJson();
    }

    public function createInstitutions(CreateInstitutionRequest $createInstitutionRequest)
    {
        $format = new InstitutionFormat($createInstitutionRequest->all());
        $this->qingfengService->createInstitution($format);
        return renderSuccessJson();
    }

    public function createQingfengProposals(CreateQingfengProposalRequest $createQingfengProposalRequest)
    {
        $format = new QingfengProposalFormat($createQingfengProposalRequest->all());
        $this->qingfengService->createQingfengProposals($format);
        return renderSuccessJson();
    }

    public function getMyQingfengProposals(Request $request)
    {
        $userId = $request->input('user_id');
        if (empty($userId)) {
            throw new ApiException('缺少参数');
        }
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $orderType = $request->input('order_type', 'desc');
        $res = $this->qingfengService->getMyQingfengProposals($userId, $page, $pageSize, $orderType);
        return renderSuccessJson($res);
    }

    public function getQingfengProposalsDetail(Request $request)
    {
        $userId = $request->input('user_id');
        if (empty($userId)) {
            throw new ApiException('缺少参数');
        }
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数');
        }
        $res = $this->qingfengService->getMyQingfengProposalDetail($userId, $id);
        return renderSuccessJson($res);
    }

    public function getAllQingfengProposals(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('accuse_name')) {
            $search['accuse_name'] = $request->input('accuse_name');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_data')) {
            $search['end_date'] = $request->input('end_date');
        }
        $res = $this->qingfengService->getAllQingfengProposals($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function getAdminQingfengProposalDetail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->qingfengService->getAdminQingfengProposalDetail($id);
        return renderSuccessJson($res);
    }

    public function updateInstitution(Request $request)
    {
        $format = new InstitutionFormat($request->all());
        $this->qingfengService->updateInstitution($format);
        return renderSuccessJson();
    }

    public function deleteInstitution(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->qingfengService->deleteInstitution($id);
        return renderSuccessJson();
    }

    public function deleteQingfengProposal(Request $request)
    {
        $id = $request->input('id');
        $this->qingfengService->deleteQingfengProposal($id);
        return renderSuccessJson();
    }
}
